import { title } from "process"

export default {
	common: {
		baseUrl: "http://123.60.140.66:88/api",
		ucenterUrl: "http://123.60.140.66:99/oauth/authorize?client_id=yrmall&response_type=code&scope=ROLE_ALL&redirect_uri=",
		data: {},
		header: {
			"Content-Type": "application/json"
		},
		method: "GET",
		dataType: "json"
	},
	request(options = {}) {
		options.url = this.common.baseUrl + options.url
		options.data = options.data || this.common.data
		options.header = options.header || this.common.header
		options.method = options.method || this.common.method
		options.dataType = options.dataType || this.common.dataType
		let loading = options.loading || false
		let loadingText = options.loadingText || '加载中'
		if(uni.getStorageSync('access_token')) {
			options.header.Authorization = "Bearer " + uni.getStorageSync('access_token')
		}
		if (loading) {
			uni.showLoading({
				mask: true,
				title: loadingText
			})
		}
		return new Promise((res,rej) => {
			uni.request({
				...options,
				success: (result) => {
					if(result.statusCode === 401 || result.data.code === 401) {
						uni.navigateTo({
							url: '/pages/authentication/authentication'
						})
					}
					if(result.data.success) {
						return res(result.data)
					}
					if(result.data) {
						return res(result.data)
					}
					uni.showToast({
						duration: 1000,
						title: '服务器连接错误',
						icon: 'none'
					})
					return rej();
				},
				complete: () => {
					uni.hideLoading()
				}
			})
		})
	}
}
